package com.tjsinfo.tjsoa.mail.bo;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.mail.dao.ITjsDraftDao;
import com.tjsinfo.tjsoa.mail.dao.ITjsFoldMailDao;
import com.tjsinfo.tjsoa.mail.vo.TjsAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmail;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmailAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsDraft;
import com.tjsinfo.tjsoa.mail.vo.TjsDraftAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsDraftReceived;
import com.tjsinfo.tjsoa.mail.vo.TjsEmailFolder;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldEmailAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldMail;
import com.tjsinfo.tjsoa.mail.vo.TjsemailReceived;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;


public class TjsDraftBo implements ITjsDraftBo{
	private final boolean STARTTRUE = true;
	private final boolean STARTFALSE = false;
	private ITjsDraftDao tjsDraftDao;
	private IQbcDao qbcDao;
	private ITjsemailReceivedBo tjsemailReceivedBo;
	private ITjsmailBo tjsmailBo;
	private ITjsDeleteEmailBo tjsDeleteEmailBo;
	private ITjsFoldMailDao tjsFoldMailDao;
	public ITjsFoldMailDao getTjsFoldMailDao() {
		return tjsFoldMailDao;
	}
	public void setTjsFoldMailDao(ITjsFoldMailDao tjsFoldMailDao) {
		this.tjsFoldMailDao = tjsFoldMailDao;
	}
	public ITjsmailBo getTjsmailBo() {
		return tjsmailBo;
	}
	public void setTjsmailBo(ITjsmailBo tjsmailBo) {
		this.tjsmailBo = tjsmailBo;
	}
	public ITjsemailReceivedBo getTjsemailReceivedBo() {
		return tjsemailReceivedBo;
	}
	public void setTjsemailReceivedBo(ITjsemailReceivedBo tjsemailReceivedBo) {
		this.tjsemailReceivedBo = tjsemailReceivedBo;
	}
	public IQbcDao getQbcDao() {
		return qbcDao;
	}
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	public ITjsDraftDao getTjsDraftDao() {
		return tjsDraftDao;
	}
	public void setTjsDraftDao(ITjsDraftDao tjsDraftDao) {
		this.tjsDraftDao = tjsDraftDao;
	}
	public ITjsDeleteEmailBo getTjsDeleteEmailBo() {
		return tjsDeleteEmailBo;
	}
	public void setTjsDeleteEmailBo(ITjsDeleteEmailBo tjsDeleteEmailBo) {
		this.tjsDeleteEmailBo = tjsDeleteEmailBo;
	}
	/**
	 * 添加草稿
	 * @param 标题
	 * @param 内容
	 * @param 时间
	 * @param 类型
	 * @param 用户ID
	 * @return 是否成功
	 */
	public boolean addTjsDraf(String title,String value,Date time,Integer type,Integer userid,String zhusong,String chaosong,String misong,List<TjsAnnex> annexarray)
	{
		try {
			TjsDraft	 tjsDraft = new TjsDraft();//声明一个草稿
			tjsDraft.setDraftDatetime(time);
			tjsDraft.setDraftTitle(title);
			tjsDraft.setDraftType(type);
			tjsDraft.setDraftValue(value);
			tjsDraft.setDraftStart(2);
			tjsDraft.setDraftTag(0);
			TSystemUser tjsUser = new TSystemUser();
			tjsUser.setUserId(userid);
			tjsDraft.setTjsUser(tjsUser);
			for (Iterator iterator = annexarray.iterator(); iterator.hasNext();) {
				TjsAnnex tjsAnnex = (TjsAnnex) iterator.next();
				TjsDraftAnnex tjsDraftAnnex = new TjsDraftAnnex();
				BeanUtilsBean utilsBean = BeanUtilsBean.getInstance();
				try {
					utilsBean.copyProperties(tjsDraftAnnex, tjsAnnex);
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				tjsDraftAnnex.setAnnexId(null);
				tjsDraft.addTjsDraftAnnexe(tjsDraftAnnex);
			}
			tjsDraftDao.save(tjsDraft);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
		
	}
	
	/**
	 * 查询一个草稿
	 * @param id
	 * @return
	 */
	public TjsDraft selectById(String id)
	{
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(TjsDraft.class);
		detachedCriteria.add(Expression.eq("tjsUser.id", id));
		List list =  qbcDao.getCriteria(detachedCriteria);
		if (list.size() > 0) {
			return (TjsDraft) list.get(0);
		} else {
			return new TjsDraft();
		}
	}
	/**
	 * 删除草稿
	 * @param id
	 * @return 
	 */
	public boolean deleteTjsDraf(String drafid)
	{
		try {
			TjsDraft tjsDraft = selectOneById(drafid);
			tjsDraftDao.delete(tjsDraft);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 删除草稿
	 * @param id
	 * @return 
	 */
	public boolean deleteTjsDraf(String[] drafidarray)
	{
		try {
			for (int i = 0; i < drafidarray.length; i++) {//根据数组批量查询草稿
				
				TjsDraft received = tjsDraftDao.selectOneById(drafidarray[i]).get(0);
				TjsDeleteEmail deleteEmail = new TjsDeleteEmail();//生成删除的邮件
				deleteEmail.setTismailRead(received.getDraftStart());
				deleteEmail.setTismailTag(received.getDraftTag());
				deleteEmail.setTjsmailDatatime(new Date());
				deleteEmail.setTjsmailStart(0);
				deleteEmail.setTjsmailTitle(received.getDraftTitle());
				deleteEmail.setTjsmailType(received.getDraftType());
				deleteEmail.setTjsmailValue(received.getDraftValue());
				deleteEmail.setTjsmailUserid(received.getTjsUser().getUserId());
				deleteEmail.setTjsmailUsername(received.getTjsUser().getUserName());
				deleteEmail.setTjsmailsendren(received.getTjsUser().getUserName());
				Set set2 = received.getTjsDraftAnnexes();//生成草稿附件
				for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
					TjsDraftAnnex annex = (TjsDraftAnnex) iterator.next();
					TjsDeleteEmailAnnex emailAnnex = new TjsDeleteEmailAnnex();
					emailAnnex.setAnnexAddress(annex.getAnnexAddress());
					emailAnnex.setAnnexName(annex.getAnnexName());
					emailAnnex.setAnnexSize(annex.getAnnexSize());
					emailAnnex.setZhenname(annex.getAnnexZhenName());
					deleteEmail.addAnnexes(emailAnnex);
				}
				tjsDeleteEmailBo.addTjsDeleteEmail(deleteEmail);
				tjsDraftDao.delete(received);
			}
				return STARTTRUE;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return STARTFALSE;
			}
	}
	/**
	 * 更新草稿
	 * @param id
	 * @param 标题
	 * @param 内容
	 * @param 类型
	 * @return
	 */
	public boolean updateTjDraf(String drafid,String title,String value,Integer type)
	{
		try {
			TjsDraft draft = selectById(drafid);
			draft.setDraftTitle(title);
			draft.setDraftDatetime(new Date());
			draft.setDraftType(type);
			draft.setDraftValue(value);
			tjsDraftDao.update(draft);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 草稿发送邮件
	 * @param 草稿ID
	 * @param 接收人ID
	 * @param 性质 1主送 2抄送 3密送
	 * @return
	 */
	public boolean sendTjDraf(String drafid,String[] sendstrarray,Integer nature,String username)
	{
		try {
			TjsDraft tjsDraft =	tjsDraftDao.selectById(drafid);//生成一个草稿
			String id ="0";
		//	Integer id = tjsmailBo.addSendMail(tjsDraft.getDraftTitle(), tjsDraft.getDraftValue(), new Date(), tjsDraft.getDraftType(), tjsDraft.getTjsUser().getId(), 0);
			for (int i = 0; i < sendstrarray.length; i++) {
				tjsemailReceivedBo.addTjsmailReceived(id, sendstrarray[i], nature, 2,username);
			}
			return STARTTRUE;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
		
	}
	/**
	 * 根据用户ID查询他的草稿
	 * @param 用户ID
	 * @return
	 */
	public List<TjsDraft> selectDraftByUserid(Integer userid) {
		// TODO Auto-generated method stub
		return tjsDraftDao.selectByUserId(userid);
	}
	/**
	 * 修改草稿的状态
	 * @param id数组
	 * @param 状态
	 * @return
	 */
	public boolean updateDraftStart(String[] array,Integer start)
	{
		try {
			for (int i = 0; i < array.length; i++) {
				
				TjsDraft tjsDraft = tjsDraftDao.selectById(array[i]);//查询一个草稿
				tjsDraft.setDraftStart(start);//改变草稿的状态
				tjsDraftDao.update(tjsDraft);
			}
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTTRUE;
		}
		return STARTTRUE;
	}
	/**
	 * 修改草稿的类型
	 * @param id数组
	 * @param 状态
	 * @return
	 */
	public boolean updateDraftType(String[] array,Integer type)
	{
		try {
			for (int i = 0; i < array.length; i++) {
				
				TjsDraft tjsDraft = tjsDraftDao.selectById(array[i]);//查询一个草稿
				tjsDraft.setDraftType(type);//改变草稿类型
				tjsDraftDao.update(tjsDraft);
			}
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTTRUE;
		}
		return STARTTRUE;
	}
	
	/**
	 * 修改草稿的标签
	 * @param id数组
	 * @param 状态
	 * @return
	 */
	public boolean updateDraftTag(String[] array,Integer tag)
	{
		try {
			for (int i = 0; i < array.length; i++) {
				
				TjsDraft tjsDraft = tjsDraftDao.selectById(array[i]);//查询草稿
				tjsDraft.setDraftTag(tag);//改变草稿的标签
				tjsDraftDao.update(tjsDraft);
			}
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTTRUE;
		}
		return STARTTRUE;
	}
	public boolean moveTjsDraf(String[] drafindarray, String foldid) {
		// TODO Auto-generated method stub
		try {
			for (int i = 0; i < drafindarray.length; i++) {
				TjsDraft received = tjsDraftDao.selectOneById(drafindarray[i]).get(0);//查询一个草稿
				TjsFoldMail tjsFoldMail = new TjsFoldMail();
				tjsFoldMail.setTjsmailRead(received.getDraftStart());
				tjsFoldMail.setTjsmailTag(received.getDraftTag());
				tjsFoldMail.setTjsmailDatetime(new Date());
				tjsFoldMail.setTjsmailStart(0);
				tjsFoldMail.setTjsmailTitle(received.getDraftTitle());
				tjsFoldMail.setTjsmailType(received.getDraftType());
				tjsFoldMail.setTjsmailValue(received.getDraftValue());
				TjsEmailFolder tjsEmailFolder = new TjsEmailFolder();
				tjsEmailFolder.setFolderId(foldid);
				tjsFoldMail.setTjsEmailFolder(tjsEmailFolder);
				tjsFoldMail.setTjsEmailFolder(tjsEmailFolder);
				//tjsFoldMail.setTjsUser(received.getTjsUser());
				tjsFoldMail.setTjsmailUsername(received.getTjsUser().getUserName());
				Set set2 = received.getTjsDraftAnnexes();//查询草稿的附件
				for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
					TjsDraftAnnex annex = (TjsDraftAnnex) iterator.next();
					TjsFoldEmailAnnex   emailAnnex = new TjsFoldEmailAnnex();//生成文件夹邮件
					emailAnnex.setAnnexAddress(annex.getAnnexAddress());
					emailAnnex.setAnnexName(annex.getAnnexName());
					emailAnnex.setAnnexSize(annex.getAnnexSize());
					emailAnnex.setAnnexZhenName(annex.getAnnexZhenName());
					tjsFoldMail.addTjsFoldMailAnnex(emailAnnex);
				}
				tjsFoldMailDao.save(tjsFoldMail);
				tjsDraftDao.delete(received);
			}
				return STARTTRUE;
			} catch (RuntimeException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return STARTFALSE;
			}
	}
	
	/**
	 * 根据草稿ID查询
	 * @param 草稿ID
	 * @return
	 */
	public TjsDraft selectOneById(String draftid)
	{
		List<TjsDraft> list = tjsDraftDao.selectOneById(draftid);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	/**
	 * 根据标题查询
	 * @param title
	 * @return
	 */
	public List<TjsDraft> selectTitle(String title,Integer userid)
	{
		Object[] object = {"%"+title+"%",userid};
		List<TjsDraft> list = tjsDraftDao.selectTitle(object);
		return list;
	}
}
